package com.front.teacher.teacherapp.view.activity.login;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.front.teacher.teacherapp.R;
import com.front.teacher.teacherapp.api.ApiService;
import com.front.teacher.teacherapp.api.RetrofitHelper;
import com.front.teacher.teacherapp.api.UrlConfig;
import com.front.teacher.teacherapp.base.BaseMvpActivity;
import com.front.teacher.teacherapp.bean.ResultEnity;
import com.front.teacher.teacherapp.presenter.LoginPresenter;
import com.front.teacher.teacherapp.utils.AESUtils;
import com.front.teacher.teacherapp.utils.SharedPreferencesHelper;
import com.front.teacher.teacherapp.view.activity.MainActivity;
import com.front.teacher.teacherapp.view.impl.ILoginView;

import java.io.IOException;

import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends BaseMvpActivity<ILoginView, LoginPresenter> implements ILoginView {

    public static final String USER_NAME = "userName";
    public static final String PASSWORD = "password";
    private SharedPreferencesHelper sharedPreferencesHelper;
    private String userName;
    private String passWord;

    @InjectView(R.id.edit_username_login)
    EditText editUsernameLogin;
    @InjectView(R.id.edit_password_login)
    EditText editPasswordLogin;
    @InjectView(R.id.forget_text_login)
    TextView forgetTextLogin;
    @InjectView(R.id.button_login)
    Button buttonLogin;

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void initView() {
        sharedPreferencesHelper = SharedPreferencesHelper.getInstance(this);
        String userName = SharedPreferencesHelper.getInstance(this).getStringValue(USER_NAME);
        String password = SharedPreferencesHelper.getInstance(this).getStringValue(PASSWORD);
        if (userName != null && password != null) {
            presenter.login(userName, password);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    //登陆成功
    @Override
    public void onSuccess() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        if (userName != null && passWord != null) {
            sharedPreferencesHelper.putStringValue(USER_NAME, userName);
            sharedPreferencesHelper.putStringValue(PASSWORD, passWord);
        }
        finish();
    }

    @Override
    public void onFail() {
        toastShow("您输入的账号或密码有误！");
    }

    @Override
    public void initData() {
        super.initData();
    }


    @OnClick({R.id.button_login, R.id.forget_text_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_login:
                userName = editUsernameLogin.getText().toString();
                passWord = editPasswordLogin.getText().toString();
                if (userName.length() == 0) {
                    toastShow("请输入用户名！");
                } else {
                    if (passWord.length() == 0) {
                        toastShow("请输入密码！");
                    } else {
                        presenter.login(userName, passWord);
                    }
                }
                break;
            case R.id.forget_text_login:
                Intent intent = new Intent(this, ForgetActivity.class);
                startActivity(intent);
                break;
        }
    }
}
